package org.skyui.beans;

import java.io.Serializable;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.Entity;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Transient;

import org.skyui.annotations.SkyField;
import org.skyui.annotations.SkyManyToMany;
import org.skyui.annotations.SkyTable;
import org.skyui.beanmanagers.BeanRelationManager;
import org.skyui.beanmanagers.BeanSetManager;
import org.skyui.beans.AbstractBean;
import org.skyui.validators.Validators;

import com.baycrypt.server.beans.networks.WLANBean;
import com.baycrypt.server.beans.people.UserBean;

@Entity
@SkyTable(itemsCaption = "Logs", selectedItemCaption = "View Log")
public class LogBean extends AbstractBean implements Serializable {

    int logLevel;

    String userEmail;

    String targetBeanId;

    Class targetBeanClass;

    String moduleName;

    String eventType;

    String action;

    String props;

    public LogBean() {
    }

    public LogBean(UserBean user, String targetBeanId, Class targetBeanClass,
	    String moduleName, String eventType, String action, int logLevel,
	    OrganizationBean organization) {
	this.setOrganization(organization);
	this.userEmail = user.getEmail();
	this.targetBeanId = targetBeanId;
	this.targetBeanClass = targetBeanClass;
	this.moduleName = moduleName;
	this.eventType = eventType;
	this.action = action;
	this.logLevel = logLevel;
    }

    private boolean isSameDay(Date d1, Date d2) {
	return (d1.getDate() == d2.getDate() && d1.getMonth() == d2.getMonth() && d1
		.getYear() == d2.getYear());
    }

    private boolean isPreviousDay(Date d1, Date d2) {
	return (d1.getDate() - 1 == d2.getDate()
		&& d1.getMonth() == d2.getMonth() && d1.getYear() == d2
		.getYear());
    }

    private String humanReadableTime(Date d) {
	Date today = new Date();
	String timeString;
	if (isSameDay(today, d))
	    timeString = " today at "
		    + d.getHours()
		    % 12
		    + ":"
		    + (d.getMinutes() > 9 ? d.getMinutes() : "0"
			    + d.getMinutes())
		    + (d.getHours() < 12 ? " am" : " pm");
	else if (isPreviousDay(today, d))
	    timeString = " yesterday at "
		    + d.getHours()
		    % 12
		    + ":"
		    + (d.getMinutes() > 9 ? d.getMinutes() : "0"
			    + d.getMinutes())
		    + (d.getHours() < 12 ? " am" : " pm");
	else
	    timeString = " on " + d.toLocaleString();
	return timeString;
    }



}